package com.xydtech.ecard.merchant.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.ecard.merchant.domain.*;
import com.xydtech.ecard.merchant.mapper.EcardBranchEcardChartMapper;
import com.xydtech.ecard.merchant.service.IEcardBranchEcardChartService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片商户信息图表统计Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-05-22 上午10:31
 */
@Service
public class EcardBranchEcardChartServiceImpl implements IEcardBranchEcardChartService {

    @Autowired
    private EcardBranchEcardChartMapper ecardBranchEcardChartMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询建档拜访统计数据
     * @return
     */
    @Override
    public JSONObject getFilingVisitsStatistics(Long userId) {
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> perFilingVisitsNums = ecardBranchEcardChartMapper.selectFilingVisitsByUserId(userId);
        statisticsData.put("perFilingNums",perFilingVisitsNums.get(0).getMerchantNum());//个人累计建档总数
        statisticsData.put("perVisitNums",perFilingVisitsNums.get(1).getMerchantNum());//个人累计拜访总数
        statisticsData.put("perDoVisitNums",perFilingVisitsNums.get(2).getMerchantNum());//个人超时拜访总数
        statisticsData.put("perUnDoVisitNums",perFilingVisitsNums.get(3).getMerchantNum());//个人超时未拜访总数
        statisticsData.put("perFilingThisMonthNums",perFilingVisitsNums.get(4).getMerchantNum());//个人本月新增建档总数
        statisticsData.put("perFilingCompareNums",perFilingVisitsNums.get(4).getMerchantNum() - perFilingVisitsNums.get(5).getMerchantNum());//个人建档较上月对比
        if (statisticsData.getLong("perFilingCompareNums") >= 0){
            statisticsData.put("perFilingType", "1");//较上月个人建档类型（1：上升，2：下降）
        } else {
            statisticsData.put("perFilingType", "2");//较上月个人建档类型（1：上升，2：下降）
        }
        statisticsData.put("perVisitThisMonthNums",perFilingVisitsNums.get(6).getMerchantNum());//个人本月新增拜访总数
        statisticsData.put("perVisitCompareNums",perFilingVisitsNums.get(6).getMerchantNum() - perFilingVisitsNums.get(7).getMerchantNum());//个人拜访较上月对比
        if (statisticsData.getLong("perVisitCompareNums") >= 0){
            statisticsData.put("perVisitType", "1");//较上月个人拜访类型（1：上升，2：下降）
        } else {
            statisticsData.put("perVisitType", "2");//较上月个人拜访类型（1：上升，2：下降）
        }
        return statisticsData;
    }

    /**
     * 查询全部的建档拜访数据
     * @return
     */
    @Override
    public JSONObject getAllFilingVisitsNums(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> allFilingVisitsNums = ecardBranchEcardChartMapper.getAllFilingVisitsNums(sysDept.getDeptId());
        statisticsData.put("filingNums",allFilingVisitsNums.get(0).getMerchantNum());//全行建档总数
        statisticsData.put("filingLastMonthNums",allFilingVisitsNums.get(1).getMerchantNum());//上月新增建档总数
        statisticsData.put("filingThisMonthNums",allFilingVisitsNums.get(2).getMerchantNum());//本月新增建档总数
        statisticsData.put("filingCompareNums",allFilingVisitsNums.get(2).getMerchantNum() - allFilingVisitsNums.get(1).getMerchantNum());//建档较上月对比
        statisticsData.put("visitNums",allFilingVisitsNums.get(3).getMerchantNum());//拜访总次数
        statisticsData.put("visitLastMonthNums",allFilingVisitsNums.get(4).getMerchantNum());//上月新增拜访总数
        statisticsData.put("visitThisMonthNums",allFilingVisitsNums.get(5).getMerchantNum());//本月新增拜访总数
        statisticsData.put("visitCompareNums",allFilingVisitsNums.get(5).getMerchantNum() - allFilingVisitsNums.get(4).getMerchantNum());//拜访较上月对比
        return statisticsData;
    }

    /**
     * 查询建档折线图数据
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getFilingLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchEcardChartMapper.getFilingLineChart(sysDept.getDeptId());
    }

    /**
     * 查询建档折线图数据（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getFilingLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchEcardChartMapper.getFilingLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 查询拜访折线图数据
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getVisitLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchEcardChartMapper.getVisitLineChart(sysDept.getDeptId());
    }

    /**
     * 查询拜访柱状图数据接口（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getVisitLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchEcardChartMapper.getVisitLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 查询超时拜访数据
     * @return
     */
    @Override
    public JSONObject getTimeoutVisitsNums(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> allTimeoutVisitsNums = ecardBranchEcardChartMapper.getTimeoutVisitsNums(sysDept.getDeptId());
        statisticsData.put("doTaskNums",allTimeoutVisitsNums.get(0).getDoTaskSum());//超时拜访总数
        statisticsData.put("doTaskLastMonthNums",allTimeoutVisitsNums.get(1).getDoTaskSum());//上月新增超时拜访总数
        statisticsData.put("doTaskThisMonthNums",allTimeoutVisitsNums.get(2).getDoTaskSum());//本月新增超时拜访总数
        statisticsData.put("doTaskCompareNums",allTimeoutVisitsNums.get(2).getDoTaskSum() - allTimeoutVisitsNums.get(1).getDoTaskSum());//超时拜访较上月对比
        statisticsData.put("undoTaskNums",allTimeoutVisitsNums.get(3).getDoTaskSum());//超时未拜访总次数
        statisticsData.put("undoTaskLastMonthNums",allTimeoutVisitsNums.get(4).getDoTaskSum());//上月新增超时未拜访总数
        statisticsData.put("undoTaskThisMonthNums",allTimeoutVisitsNums.get(5).getDoTaskSum());//本月新增超时未拜访总数
        statisticsData.put("undoTaskCompareNums",allTimeoutVisitsNums.get(5).getDoTaskSum() - allTimeoutVisitsNums.get(4).getDoTaskSum());//超时未拜访较上月对比
        return statisticsData;
    }

    /**
     * 机构商户建档排名
     * @param ecardBranchDeptFiling
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchDeptFiling> getDeptFilingList(EcardBranchDeptFiling ecardBranchDeptFiling) {
        return ecardBranchEcardChartMapper.getDeptFilingList(ecardBranchDeptFiling);
    }

    /**
     * 机构商户建档排名（小程序接口）
     * @param ecardBranchDeptFiling
     * @return
     */
    @Override
    public List<EcardBranchDeptFiling> getDeptFilingListByApplet(EcardBranchDeptFiling ecardBranchDeptFiling) {
        return ecardBranchEcardChartMapper.getDeptFilingListByApplet(ecardBranchDeptFiling);
    }

    /**
     * 机构拜访次数排名
     * @param ecardBranchDeptVisit
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchDeptVisit> getDeptVisitsList(EcardBranchDeptVisit ecardBranchDeptVisit) {
        return ecardBranchEcardChartMapper.getDeptVisitsList(ecardBranchDeptVisit);
    }

    /**
     * 机构拜访次数排名（小程序接口）
     * @param ecardBranchDeptVisit
     * @return
     */
    @Override
    public List<EcardBranchDeptVisit> getDeptVisitsListByApplet(EcardBranchDeptVisit ecardBranchDeptVisit) {
        return ecardBranchEcardChartMapper.getDeptVisitsListByApplet(ecardBranchDeptVisit);
    }

    /**
     * 机构超时拜访次数排名
     * @param ecardBranchDeptDoTask
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchDeptDoTask> getDeptDoTaskList(EcardBranchDeptDoTask ecardBranchDeptDoTask) {
        return ecardBranchEcardChartMapper.getDeptDoTaskList(ecardBranchDeptDoTask);
    }

    /**
     * 机构超时拜访次数排名（小程序接口）
     * @param ecardBranchDeptDoTask
     * @return
     */
    @Override
    public List<EcardBranchDeptDoTask> getDeptDoTaskListByApplet(EcardBranchDeptDoTask ecardBranchDeptDoTask) {
        return ecardBranchEcardChartMapper.getDeptDoTaskListByApplet(ecardBranchDeptDoTask);
    }

    /**
     * 机构超时未拜访次数排名
     * @param ecardBranchDeptUndoTask
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchDeptUndoTask> getDeptUnDoTaskList(EcardBranchDeptUndoTask ecardBranchDeptUndoTask) {
        return ecardBranchEcardChartMapper.getDeptUnDoTaskList(ecardBranchDeptUndoTask);
    }

    /**
     * 机构超时未拜访次数排名（小程序接口）
     * @param ecardBranchDeptUndoTask
     * @return
     */
    @Override
    public List<EcardBranchDeptUndoTask> getDeptUnDoTaskListByApplet(EcardBranchDeptUndoTask ecardBranchDeptUndoTask) {
        return ecardBranchEcardChartMapper.getDeptUnDoTaskListByApplet(ecardBranchDeptUndoTask);
    }

    /**
     * 营销人员商户建档排名
     * @param ecardBranchUserFiling
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchUserFiling> getUserFilingList(EcardBranchUserFiling ecardBranchUserFiling) {
        return ecardBranchEcardChartMapper.getUserFilingList(ecardBranchUserFiling);
    }

    /**
     * 营销人员商户建档排名（小程序接口）
     * @param ecardBranchUserFiling
     * @return
     */
    @Override
    public List<EcardBranchUserFiling> getUserFilingListByApplet(EcardBranchUserFiling ecardBranchUserFiling) {
        return ecardBranchEcardChartMapper.getUserFilingListByApplet(ecardBranchUserFiling);
    }

    /**
     * 营销人员拜访次数排名
     * @param ecardBranchUserVisit
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchUserVisit> getUserVisitsList(EcardBranchUserVisit ecardBranchUserVisit) {
        return ecardBranchEcardChartMapper.getUserVisitsList(ecardBranchUserVisit);
    }

    /**
     * 营销人员拜访次数排名（小程序接口）
     * @param ecardBranchUserVisit
     * @return
     */
    @Override
    public List<EcardBranchUserVisit> getUserVisitsListByApplet(EcardBranchUserVisit ecardBranchUserVisit) {
        return ecardBranchEcardChartMapper.getUserVisitsListByApplet(ecardBranchUserVisit);
    }

    /**
     * 营销人员超时拜访次数排名
     * @param ecardBranchUserDoTask
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchUserDoTask> getUserDoTaskList(EcardBranchUserDoTask ecardBranchUserDoTask) {
        return ecardBranchEcardChartMapper.getUserDoTaskList(ecardBranchUserDoTask);
    }

    /**
     * 营销人员超时拜访次数排名（小程序接口）
     * @param ecardBranchUserDoTask
     * @return
     */
    @Override
    public List<EcardBranchUserDoTask> getUserDoTaskListByApplet(EcardBranchUserDoTask ecardBranchUserDoTask) {
        return ecardBranchEcardChartMapper.getUserDoTaskListByApplet(ecardBranchUserDoTask);
    }

    /**
     * 营销人员超时未拜访次数排名
     * @param ecardBranchUserUndoTask
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardBranchUserUndoTask> getUserUnDoTaskList(EcardBranchUserUndoTask ecardBranchUserUndoTask) {
        return ecardBranchEcardChartMapper.getUserUnDoTaskList(ecardBranchUserUndoTask);
    }

    /**
     * 营销人员超时未拜访次数排名（小程序接口）
     * @param ecardBranchUserUndoTask
     * @return
     */
    @Override
    public List<EcardBranchUserUndoTask> getUserUnDoTaskListByApplet(EcardBranchUserUndoTask ecardBranchUserUndoTask) {
        return ecardBranchEcardChartMapper.getUserUnDoTaskListByApplet(ecardBranchUserUndoTask);
    }
}
